package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.GradeBean;
import com.project.bean.RoleBean;
import com.project.mapper.IRoleMapper;
import com.project.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class RoleServiceImp implements IRoleService {
    @Autowired
    private IRoleMapper roleMapper;

    @Override
    public void updateRole(Integer userId, Integer[] roleIdArray) {
        roleMapper.updateRole(userId,roleIdArray);
    }

    @Override
    public List<RoleBean> findAll() {
        return roleMapper.selectList(null);
    }

    @Override
    public IPage<RoleBean> cutAll(Integer pageNO) {
        return roleMapper.selectPage(new Page<>(pageNO,5),null);
    }

    @Override
    public void add(String roleName, Integer[] gradeIdArray) {
        RoleBean roleBean = new RoleBean();
        roleBean.setName(roleName);
        roleMapper.insert(roleBean);
        roleMapper.add(roleBean.getId(),gradeIdArray);
    }

    @Override
    public void del(Integer roleId) {
        roleMapper.del(roleId);
    }

    @Override
    public RoleBean findById(Integer roleId) {
        RoleBean roleBean =roleMapper.selectById(roleId);
        //查询权限数组、set进角色属性里
        roleBean.setGradeList(roleMapper.findById(roleId));
        return  roleBean;
    }

    @Override
    public void updateGrade(Integer roleId, Integer[] gradeIdArray) {
        roleMapper.updateGrade(roleId,gradeIdArray);
    }
}
